package com.haitonghop.requester;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.haitonghop.data.HaiBLMetalData;
import com.haitonghop.data.HaiBLModule;
import com.haitonghop.data.TamDiemDetailModuleData;
import com.haitonghop.data.TamDiemDetailTopicData;
import com.haitonghop.data.TamDiemMetalData;
import com.haitonghop.network.Request;
import com.haitonghop.network.RequestListener;
import com.haitonghop.network.UrlHelper;
import com.haitonghop.utils.AppSettingParams;
import com.haitonghop.utils.Logger;

public class TamDiemRequester extends Request{

	public static final int TYPE_GET_LIST = 1000;
	public static final int TYPE_GET_DETAIL = 1002;
	public static final int TYPE_GET_IMAGE_LIST = 1003;
	private int type = TYPE_GET_LIST;
    
    public static final String METHOD_WORLD = "world";
    public static final String METHOD_VIETNAM = "vietnam";
    public static final String METHOD_RELAX = "relax";
    
    public static final String METHOD_DOCUMENT = "doc";
    public static final String METHOD_IEADS = "ideas";
    public static final String METHOD_MYEYE = "myeye";
    
    public static final String METHOD_TOUR = "tour";
    public static final String METHOD_FUNNY = "funny";
    public static final String METHOD_GIRL = "girl";
    
    public static final String METHOD_FOCUS = "focus";
    public static final String METHOD_DISCOVERY = "discovery";

	private String method = "";
	private int page = 1;
	private String href = "";
	
	private RequestListener requestListener;
	
	static public TamDiemRequester getImageList(String href, RequestListener requestListener){
		TamDiemRequester diemRequester = new TamDiemRequester();
		diemRequester.setType(TYPE_GET_IMAGE_LIST);
		diemRequester.setMethod("detail");
		diemRequester.setHref(href);
		diemRequester.setRequestListener(requestListener);
		return diemRequester;
	}
	
	static public TamDiemRequester getDetailModules(String idModules,int page, RequestListener requestListener){
		TamDiemRequester diemRequester = new TamDiemRequester();
		diemRequester.setType(TYPE_GET_DETAIL);
		diemRequester.setPage(page);
		diemRequester.setMethod(idModules);
		diemRequester.setRequestListener(requestListener);
		return diemRequester;
	}
	
	
	//method getlist	
	static public TamDiemRequester getLists(RequestListener requestListener){
		TamDiemRequester diemRequester = new TamDiemRequester();
		diemRequester.setType(TYPE_GET_LIST);
		diemRequester.setMethod("getlist");
		diemRequester.setRequestListener(requestListener);
		return diemRequester;
	}
	
	//Data
	TamDiemDetailTopicData detailTopicData = null;
	ArrayList<TamDiemMetalData> haiVLMetalDatas = new ArrayList<TamDiemMetalData>();
	ArrayList<TamDiemDetailModuleData> tamdiemModulesDetailList = new ArrayList<TamDiemDetailModuleData>();
	public void setMethod(String aMethod){
		this.method = aMethod;
	}
	
	public void setPage(int page){
		this.page = page;
	}
	
	public void setRequestListener(RequestListener requestListener){
		this.requestListener = requestListener;
	}

	@Override
	public void cancelRequest() {
		// TODO Auto-generated method stub		
	}

	@Override
	public void runRequest() {		
		if(type == TYPE_GET_IMAGE_LIST){
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_TAMDIEM_DETAIL,
					new String[]{"method","url"},
					new String[]{ method , href});
		} else {
			url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_TAMDIEM,
					new String[]{"method","page"},
					new String[]{ method , String.valueOf(page)});
		}
			
			send();		
//		// TODO Auto-generated method stub
//		url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_TAMDIEM,
//				new String[]{"method","id"               ,"count"},
//				new String[]{ method , String.valueOf(page),String.valueOf(AppSettingParams.COUNT)});
//		send();
	}
	
	public ArrayList<TamDiemMetalData> getDataList(){
		return haiVLMetalDatas;
	}
	
	public ArrayList<TamDiemDetailModuleData> getDataDetailList(){
		return tamdiemModulesDetailList;
	}
	
	public TamDiemDetailTopicData getImageLists(){
		return detailTopicData;
	}
	
	@Override
	public void requestFinished(JSONObject jsonObject) {
		if(jsonObject != null && jsonObject.has("data")){
			
			if(type == TYPE_GET_IMAGE_LIST){
				try{
					JSONObject datas = jsonObject.getJSONObject("data");
					String title = datas.has(TamDiemDetailTopicData.TITLE) ? datas.getString(TamDiemDetailTopicData.TITLE) : "";
					String descrition = datas.has(TamDiemDetailTopicData.DESCRIPTION) ? datas.getString(TamDiemDetailTopicData.DESCRIPTION) : "";
					String url = datas.has(TamDiemDetailTopicData.URL) ? datas.getString(TamDiemDetailTopicData.URL) : "";
					
					detailTopicData = new TamDiemDetailTopicData();
					detailTopicData.setTitle(title);
					detailTopicData.setDescrition(descrition);
					detailTopicData.setBaseURL(url);
					
					JSONArray array = datas.has("images") ? datas.getJSONArray("images") : null;
					if(array != null){
						final int size = array.length();
						for(int i = 0; i < size ; i++){					
							JSONObject image = array.getJSONObject(i);
							
							String src = image.has("src") ? image.getString("src") : "";
							String caption = image.has("caption") ? image.getString("caption") : "";
							
							HaiBLMetalData blMetalData = new HaiBLMetalData();
							blMetalData.setTYPE(HaiBLModule.TAMDIEM);
							blMetalData.setImageUrl(src);
							blMetalData.setTitleDescription(caption);
							detailTopicData.appendHaiBlMetalData(blMetalData);
						}
					}
				} catch(JSONException e){
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(requestListener != null){
					requestListener.onRequestComplete(this);
				}
			} else {
				try {
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					final int size = jsonArray.length();
					for(int i = 0; i < size ; i++){					
						JSONObject datas = jsonArray.getJSONObject(i);
						
						if(type == TYPE_GET_DETAIL){
							String title = datas.has(TamDiemDetailModuleData.TITLE) ? datas.getString(TamDiemDetailModuleData.TITLE) : "";
							String avatar = datas.has(TamDiemDetailModuleData.AVATAR) ? datas.getString(TamDiemDetailModuleData.AVATAR) : "";
							String href = datas.has(TamDiemDetailModuleData.HREF) ? datas.getString(TamDiemDetailModuleData.HREF) : "";
							
							TamDiemDetailModuleData detailModuleData = new TamDiemDetailModuleData(title, avatar, href);
							tamdiemModulesDetailList.add(detailModuleData);
							
						} else if(type == TYPE_GET_LIST){
							String id = datas.has(TamDiemMetalData.ID) ? datas.getString(TamDiemMetalData.ID) : "";
							String name = datas.has(TamDiemMetalData.NAME) ? datas.getString(TamDiemMetalData.NAME) : "";
							String caption = datas.has(TamDiemMetalData.CAPTION) ? datas.getString(TamDiemMetalData.CAPTION) : "";
							String image = datas.has(TamDiemMetalData.IMAGE) ? datas.getString(TamDiemMetalData.IMAGE) : "";
							TamDiemMetalData haiBLMetalData = new TamDiemMetalData(id, name,caption);
							haiBLMetalData.setImage(image);
							haiVLMetalDatas.add(haiBLMetalData);
						}
					}
					
					if(requestListener != null){
						requestListener.onRequestComplete(this);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void requestFailed(int errorCode, String message) {
		// TODO Auto-generated method stub
		if(requestListener != null){
			requestListener.onRequestFailed(message, this);
		}
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

}
